package com.example.demo.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.util.GetTokenUtil;
import com.example.util.HttpClientUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoController {

    public static void main1(String[] args) throws Exception {
        String name="王兵";
        String pointId="1466376754494664705";
        String id;//用户id
        String departmentId;//用户id
        String deptName;//部门名称
        String deptId;//部门id;
        ArrayList<Object> userData = new ArrayList<>();//人员选择组件字段数据
        ArrayList<Object> deptData = new ArrayList<>();//部门选择组件字段数据
        String replayToken;//校验码

        GetTokenUtil getTokenUtil = new GetTokenUtil();
        String token = getTokenUtil.getToken();
        //System.out.println(token);
        // user_url 根据姓名获取用户信息 云枢接口
        String user_url = "http://221.3.192.248:8080/api/api/organization/user/search";
        //Map<String, Object> params = new HashMap<>();
        JSONObject params = new JSONObject();
        params.put("wd",name);
        params.put("page",1);
        params.put("size",10);
//        RestTemplate client = new RestTemplate();
//        client.setUriTemplateHandler("DASD");
        String user = HttpClientUtils.doPostJson1(user_url, String.valueOf(params),token);
        //System.out.println("user========="+user);
        //解析出用户id
        Map<String, Object> parse = (Map<String, Object>) JSONArray.parse(user);
        Object data = parse.get("data");
        Map<String, Object> parse1 =(Map<String, Object>) JSONArray.parse( data.toString());
        Object content = parse1.get("content");
        List parse2 =(List) JSONArray.parse( content.toString());
        //System.out.println(parse2.toString());
        Object parse3 = parse2.get(0);
        Map<String, String> parse4 =(Map<String, String>) JSONArray.parse(parse3.toString());
        id = parse4.get("id");
        departmentId = parse4.get("departmentId");
        //System.out.println(id);

        //使用用户id获取部门信息
        String deptURL = "http://221.3.192.248:8080/api/api/organization/user/departments";
        String dept = HttpClientUtils.doGetDeptById(deptURL, token, id);
        //System.out.println(dept);
        //解析出部门名称与id
        Map<String, Object> deptMap1 = (Map<String, Object>) JSONArray.parse(dept);
        Object data1 = deptMap1.get("data");
        //System.out.println(data1);
        List deptList =(List) JSONArray.parse( data1.toString());
        Object deptList1 = deptList.get(0);
        Map<String, String> deptList2 =(Map<String, String>) JSONArray.parse(deptList1.toString());
        String deptName1 = deptList2.get("deptName");
        deptId = deptList2.get("deptId");
        String[] split = deptName1.split("/");
        deptName=split[split.length-1];
        //System.out.println(split[split.length-1]);
        //构造人员选择组件字段数据格式
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("id",id);
        userMap.put("type",3);
        userMap.put("name",name);
        userData.add(userMap);
        //构造部门选择组件字段数据格式
        HashMap<String, Object> deptMap = new HashMap<>();
        deptMap.put("id",deptId);
        deptMap.put("type",1);
        deptMap.put("name",deptName);
        deptData.add(deptMap);

        //调用云枢接口获取replayToken（校验码）
        //获取重放校验码接口
        String replayTokenURL ="http://221.3.192.248:8080/api/api/runtime/form/getReplayToken";
        String replayToken1 = HttpClientUtils.doGetReplayToken(replayTokenURL, token);
        //System.out.println("replayToken==="+replayToken);
        Map<String, Object> replayTokenMap = (Map<String, Object>) JSONArray.parse(replayToken1);
        Object replayTokenData = replayTokenMap.get("data");
        replayToken = replayTokenData.toString();
        //System.out.println(replayTokenData.toString());

        //构建data
        HashMap<String, Object> submitData = new HashMap<>();
        submitData.put("StaffSelector1608775420787",deptData);
        submitData.put("StaffSelector1608775349969",userData);
        submitData.put("ShortText1608775298320","测试上报类型");
        submitData.put("ShortText1608775466451","测试问题类型");
        submitData.put("ShortText1608775481989","测试区域");
        submitData.put("address","测试位置");
        submitData.put("ShortText1639135208098",pointId);



        HashMap<String, Object> bizObject = new HashMap<>();
        bizObject.put("data",submitData);
        bizObject.put("schemaCode","WB_1w1mtb");//模型编码
        bizObject.put("sheetCode","ssb_form_data");//表单编码
        //bizObject.put("sheetCode","ssb_qrcode");//表单编码


        //构建submit接口参数
        //HashMap<String, Object> submitMap = new HashMap<>();
        JSONObject submitJson = new JSONObject();
        submitJson.put("actionCode","submit");
        submitJson.put("agree",true);
        submitJson.put("bizObject",bizObject);
        submitJson.put("formType","2");
        submitJson.put("replayToken",replayToken);
        System.out.println(submitJson);

        //调用submit接口
        String submitURL = "http://221.3.192.248:8080/api/api/runtime/form/submit";
        String submit = HttpClientUtils.doPostSubmit(submitURL, String.valueOf(submitJson),token);

        System.out.println(submit);

    }


    @SneakyThrows
    public static void main(String[] args) {
        String url = "http://acl.iansn.com/el.html?f=1KByr1SDtYY&address_val=四川省发达&area_val=各省市地方&res_org_val_id=2c2c808678d54efa0178ee0a2b3f46c3&res_org_val_unitType=3&res_org_val_name=测试小组&duty_officer_val_id=95d223529e0b4cd092e146eb83390394&duty_officer_val_unitType=1&duty_officer_val_name=王兵";
        //修正反斜杠为斜杠
        url = url.replace("\\", "/");
        //使用长文本代替要保留字符串
        url = url.replace(":", "_*colon*_")
                .replace("/", "_*slash*_")
                .replace("\\", "_*backslash*_")
                .replace(" ", "_*blank*_")
                .replace("?", "_*question*_")
                .replace("=", "_*equal*_")
                .replace("&", "_*with*_")
                .replace(";", "_*semicolon*_");


        try {
            url = URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        url = URLEncoder.encode(url, "utf-8");

        url = url.replace("_*colon*_", ":")
                .replace("_*slash*_", "/")
                .replace("_*backslash*_", "\\")
                .replace("_*blank*_", "%20")
                .replace("_*question*_", "?")
                .replace("_*equal*_", "=")
                .replace("_*with*_", "&")
                .replace("_*semicolon*_", ";");

        System.out.println(url);
    }


}
